﻿
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class UIBattleLog : UIObject
{
    public GameObject gridRoot;
    public XIncubator incubator;
    protected override void Awake()
    {
        base.Awake();
        _sb = new StringBuilder();
    }

    private DataBattleMgr _dataBattleMgr;
    public void Bind(DataBattleMgr data)
    {
        _dataBattleMgr = data;
    }

    public void Unbind()
    {
        _dataBattleMgr = null;

        //删除
        for (int i = gridRoot.transform.childCount - 1; i > -1 ; i--)
        {
            var trans = gridRoot.transform.GetChild(i);
            XGameObject.Destroy(trans.gameObject);
        }
    }

    protected override void onVisible()
    {

        //有玩家做操作
        Facade.Instance.RegistNetEvent(nameof(notify_chat), OnHandleNotifyChat);
        Facade.Instance.RegistNetEvent(nameof(notify_voteclear), OnHandleVoteClear);
        Facade.Instance.RegistNetEvent(nameof(notify_exam), OnHandleNotifyExam);
        Facade.Instance.RegistNetEvent(nameof(notify_dealcard), OnHandleNotifyDealcard);
    }

    protected override void OnInvisible()
    {

        //有玩家做操作
        Facade.Instance.UnRegistNetEvent(nameof(notify_chat), OnHandleNotifyChat);
        Facade.Instance.UnRegistNetEvent(nameof(notify_voteclear), OnHandleVoteClear);
        Facade.Instance.UnRegistNetEvent(nameof(notify_exam), OnHandleNotifyExam);
        Facade.Instance.UnRegistNetEvent(nameof(notify_dealcard), OnHandleNotifyDealcard);
    }

    StringBuilder _sb;
    //数据处理中心
    private void OnHandleNotifyChat(System.Object rpcReq)
    {
        var rsp = rpcReq as notify_chat;
        ShowLog(rsp.uid, nameof(notify_chat), rsp.msg);
    }

    private void OnHandleVoteClear(System.Object rpcReq)
    {
        var rsp = rpcReq as notify_voteclear;
        ShowLog(rsp.uid, nameof(notify_voteclear), null);
    }

    private void OnHandleNotifyExam(System.Object rpcReq)
    {
        var rsp = rpcReq as notify_exam;
        ShowLog(rsp.uid, nameof(notify_exam), rsp.type.ToString());
    }

    private void OnHandleNotifyDealcard(System.Object rpcReq)
    {
        var rsp = rpcReq as notify_dealcard;
        ShowLog(rsp.uid, nameof(notify_dealcard), null);
    }

    private void ShowLog(string uid, string notifyName, string msg)
    {
        XFactory.Instance.CreateInstanceAsync("UIBattleLogItem", incubator, (GameObject obj)
        => {

            _sb.Clear();

            _sb.Append(System.DateTime.Now.Hour).Append(":").Append(System.DateTime.Now.Minute).Append(":").Append(System.DateTime.Now.Second);
            var data = _dataBattleMgr.GetDataPlayer(uid);
            if (data != null)
                _sb.Append(" <color=#0000ff>").Append(data.GetUserName()).Append("</color> ");
            else
                _sb.Append(" <color=#0000ff>").Append("***").Append("</color> ");

            switch (notifyName)
            {
                case nameof(notify_chat):
                    _sb.Append(XLocalization.Get("说:"));
                    _sb.Append(msg);
                    break;
                case nameof(notify_voteclear):
                    _sb.Append(XLocalization.Get("清除了所有投票"));
                    break;
                case nameof(notify_dealcard):
                    _sb.Append(XLocalization.Get("给大家发牌"));
                    break;
                case nameof(notify_exam):
                    _sb.Append(XLocalization.Get("验投票结果 type:" + msg));
                    break;
                default:
                    Debug.LogError("unhandle notify name:" + notifyName);
                    break;
            }
           


            //rander
            obj.transform.SetParent(gridRoot.transform, false);
            obj.transform.SetSiblingIndex(0);
            var text = obj.GetComponent<Text>();
            text.text = _sb.ToString();

            //太多了，删除一些
            if (gridRoot.transform.childCount > 50)
            {
                XGameObject.Destroy(gridRoot.transform.GetChild(gridRoot.transform.childCount - 1).gameObject);
            }
        });


    }
}
